from rest_framework import serializers

from apps.goods.models import SPUSpecification, SPU


class SPUSpecificationSerializer(serializers.ModelSerializer):
    '''
        商品规格表序列化器
    '''
    # 关联对象嵌套序列化返回 子表嵌套父表返回 StringRelatedField返回关联表的str方法的结果
    spu = serializers.StringRelatedField(read_only=True)
    spu_id = serializers.IntegerField()
    class Meta:
        # 指定模型类
        model = SPUSpecification
        fields = ('id', 'name', 'spu', 'spu_id')

class SPUSerializer(serializers.ModelSerializer):
    '''
        SPU商品序列化器
    '''
    class Meta:
        # 指定模型类
        model = SPU
        fields = ('id', 'name')